package com.uuj.project.uiview;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.uuj.project.R;
import com.uuj.project.listener.IToolBarCallBackListener;


/**
 * ProjectName:    Ok_RxJava_Retrofit_Mvp
 * Package:        com.ok.java.retrofit
 * ClassName:      UIToolBar
 * Description:    自定义标题栏
 * Author:         张继
 * CreateDate:     2019/4/24 22:37
 * UpdateUser:     更新者
 * UpdateDate:     2019/4/24 22:37
 * UpdateRemark:   更新说明
 * Version:        1.0
 */
public class UIToolBar extends RelativeLayout implements View.OnClickListener {

    private LinearLayout mImgLeftClick, mTxtLeftClick, mImgRightClick, mTxtRightClick;
    private ImageView mImgLeft, mImgRight;
    private TextView mTitle, mTxtLeft, mTxtRight;
    private int INVALID_RES = -1;
    private IToolBarCallBackListener mToolButtonClickListener;

    public UIToolBar(@NonNull Context context) {
        this(context, null);
    }

    public UIToolBar(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public UIToolBar(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    private void init(Context context, AttributeSet attrs) {
        LayoutInflater.from(context).inflate(R.layout.layout_toolbar, this, true);
        // 返回点击事件
        mImgLeftClick = findViewById(R.id.toolbar_image_left_click);
        mImgLeftClick.setOnClickListener(this);
        // 返回图标
        mImgLeft = findViewById(R.id.toolbar_image_left);
        // 返回点击事件
        mTxtLeftClick = findViewById(R.id.toolbar_txt_left_click);
        mTxtLeftClick.setOnClickListener(this);
        // 返回text
        mTxtLeft = findViewById(R.id.toolbar_txt_left);
        // 标题栏
        mTitle = findViewById(R.id.toolbar_title);
        // 确认点击事件
        mImgRightClick = findViewById(R.id.toolbar_image_right_click);
        mImgRightClick.setOnClickListener(this);
        // 确认图标
        mImgRight = findViewById(R.id.toolbar_image_right);
        mImgRight.setOnClickListener(this);
        // 确认点击事件
        mTxtRightClick = findViewById(R.id.toolbar_txt_right_click);
        mImgRightClick.setOnClickListener(this);
        // 确认text
        mTxtRight = findViewById(R.id.toolbar_txt_right);
        mTxtRight.setOnClickListener(this);

        // 绑定属性
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.UIToolBar);
        setLeftImage(typedArray.getResourceId(R.styleable.UIToolBar_leftImage, INVALID_RES));
        setLeftText(typedArray.getString(R.styleable.UIToolBar_leftText));
        setTitle(typedArray.getString(R.styleable.UIToolBar_toolTitle));
        setTitleColor(typedArray.getColor(R.styleable.UIToolBar_toolTitleTextColor, 0xffffff));
        setTitleSize(typedArray.getDimension(R.styleable.UIToolBar_toolTitleTextSize, 18.0f));
        setRightImage(typedArray.getResourceId(R.styleable.UIToolBar_rightImage, INVALID_RES));
        setRightText(typedArray.getString(R.styleable.UIToolBar_rightText));
        setNavTextColor(typedArray.getColor(R.styleable.UIToolBar_navTextColor, 0xffffff));
        setNavTextSize(typedArray.getDimension(R.styleable.UIToolBar_navTextSize, 15.0f));
        typedArray.recycle();
    }

    /**
     * 设置左右文字的大小
     *
     * @param dimension 文字大小
     */
    private void setNavTextSize(float dimension) {
        if (dimension != INVALID_RES) {
            mTxtLeft.setTextSize(dimension);
            mTxtRight.setTextSize(dimension);
        }
    }

    /**
     * 设置左右文字的颜色
     *
     * @param color 颜色
     */
    private void setNavTextColor(int color) {
        if (color != INVALID_RES) {
            mTxtLeft.setTextColor(color);
            mTxtRight.setTextColor(color);
        }
    }

    /**
     * 设置右边的文字
     *
     * @param content 文字内容
     */
    public void setRightText(String content) {
        if (!TextUtils.isEmpty(content)) {
            mTxtRight.setText(content);
            setVisible(mTxtRightClick, true);
        } else {
            setVisible(mTxtRightClick, false);
        }
    }

    /**
     * 设置右边的图片
     *
     * @param resourceId 图片资源id
     */
    private void setRightImage(int resourceId) {
        if (resourceId != INVALID_RES) {
            mImgRight.setImageResource(resourceId);
            setVisible(mImgRightClick, true);
        } else {
            setVisible(mImgRightClick, false);
        }
    }

    /**
     * 设置标题的大小
     *
     * @param dimension 大小
     */
    private void setTitleSize(float dimension) {
        mTitle.setTextSize(dimension);
    }

    /**
     * 设置标题的颜色
     *
     * @param color 颜色
     */
    private void setTitleColor(int color) {
        mTitle.setTextColor(color);
    }

    /**
     * 设置标题
     *
     * @param content 内容
     */
    public void setTitle(String content) {
        if (!TextUtils.isEmpty(content)) {
            mTitle.setText(content);
            setVisible(mTitle, true);
        } else {
            setVisible(mTitle, false);

        }
    }

    /**
     * 设置左边的文字
     *
     * @param content 内容
     */
    private void setLeftText(String content) {
        if (!TextUtils.isEmpty(content)) {
            mTxtLeft.setText(content);
            setVisible(mTxtLeftClick, true);
        } else {
            setVisible(mTxtLeftClick, false);
        }
    }

    /**
     * 设置返回图片
     *
     * @param resourceId 图片资源id
     */
    private void setLeftImage(int resourceId) {
        if (resourceId != INVALID_RES) {
            mImgLeft.setImageResource(resourceId);
            setVisible(mImgLeftClick, true);
        } else {
            setVisible(mImgLeftClick, false);
        }
    }

    /**
     * 是否隐藏控件
     *
     * @param v       控件
     * @param visible true显示 false隐藏
     */
    private void setVisible(View v, boolean visible) {
        if (visible) {
            v.setVisibility(VISIBLE);
        } else {
            v.setVisibility(INVISIBLE);
        }
    }

    /**
     * 设置点击事件
     *
     * @param listener 监听
     */
    public void setToolBarClickListener(IToolBarCallBackListener listener) {
        this.mToolButtonClickListener = listener;
    }

    @Override
    public void onClick(View v) {
        if (mToolButtonClickListener == null) {
            return;
        }
        if (v.getId() == R.id.toolbar_image_left_click || v.getId() == R.id.toolbar_txt_left_click) {
            mToolButtonClickListener.onLeftClick();
        } else {
            mToolButtonClickListener.onRightClick();
        }
    }
}
